package com.our.sys.project1.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.web.UserSession;
import com.our.sys.common.utils.AppUtil;
import com.our.sys.project1.RunConstants;
import com.our.sys.project1.entity.StockDetail;
import com.our.sys.project1.entity.StockIn;
import com.our.sys.project1.entity.SupplierType;
import com.our.sys.project1.service.StockService;

@Controller
@RequestMapping("/stock_in")
public class StockInController {

    @Autowired
    private StockService stockService;

    private static Logger logger = LoggerFactory.getLogger(StockInController.class);

    @RequestMapping(value = "/query_stock_in")
    public @ResponseBody
    Map<String, Object> queryStockIn(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<StockIn> page = new Page<StockIn>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("inTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();

        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_stockNo_OR_number_OR_supplier", name.trim());
            filters.add(filter);
        }

        PropertyFilter filter = new PropertyFilter("EQS_state", RunConstants.STOCK_NOT_CHECK);
        filters.add(filter);

        Page<StockIn> data = stockService.queryStockIn(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query_fresh")
    public @ResponseBody
    Map<String, Object> queryFresh(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<StockIn> page = new Page<StockIn>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("inTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }
        
        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();

        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_stockNo_OR_number_OR_supplier", name.trim());
            filters.add(filter);
        }

        PropertyFilter filter = new PropertyFilter("EQS_state", RunConstants.STOCK_CHECK);
        filters.add(filter);

        Page<StockIn> data = stockService.queryStockIn(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query_stock_detail")
    public @ResponseBody
    Map<String, Object> queryStockDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<StockDetail> page = new Page<StockDetail>(request);

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        String stockInId = request.getParameter("stockInId");
        PropertyFilter filter = new PropertyFilter("EQL_stockInId", stockInId);
        filters.add(filter);

        Page<StockDetail> data = stockService.queryStockDetail(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/validation")
    public @ResponseBody
    Map<String, Object> validation(HttpServletRequest request, SupplierType supplierType) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String stockInId = request.getParameter("stockInId");
        try {
            stockService.validation(Long.valueOf(stockInId));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/fresh")
    public @ResponseBody
    Map<String, Object> fresh(HttpServletRequest request, SupplierType supplierType) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String stockInId = request.getParameter("stockInId");
        UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        try {
            stockService.fresh(Long.valueOf(stockInId), u.getAccount());
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
}
